package com.dragon.graduation.file;

import com.dragon.config.crossConfig.NoToken;
import com.dragon.results.result.HttpResult;
import com.dragon.utils.LocalFileUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件控制接口
 *
 * @author dragon
 * @since 2025/3/22 12:41
 */
@Api(tags = "文件控制")
@RestController
@RequestMapping("file")
@RequiredArgsConstructor
public class FileController {
    private final LocalFileUtil localFileUtil;

    @PostMapping("upload")
    @ApiOperation("1]文件上传")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile", paramType = "form"),
            @ApiImplicitParam(name = "bucket", value = "桶", required = true, dataType = "String")
    })
    @NoToken
    public HttpResult<String> upload(@RequestPart MultipartFile file,String bucket){
        String report = localFileUtil.upload(file, bucket);
        return HttpResult.okForData(report);
    }

    @GetMapping("delete")
    @ApiOperation("2]文件删除")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "fileName", value = "文件名/文件url", required = true, dataType = "String")
    public HttpResult<String> delete(String fileName){
        localFileUtil.deleteFile(fileName);
        return HttpResult.ok("删除成功");
    }
}
